﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BindableForm.cs" company="">
//   
// </copyright>
// <summary>
//   The bindable form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Controls
{
	using System;
	using System.Windows.Forms;
	using Libium.Core;
	using Libium.UseCases;
	using Libium.Views;
	using Libium.WindowsForms.Controls;

    // TODO Add CaptureGlobalMouse
    /// <summary>
    /// The bindable form.
    /// </summary>
    public partial class BindableForm : Form, IView
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BindableForm"/> class.
        /// </summary>
        protected BindableForm()
        {
            InitializeComponent();
            DragHandler = new DragHandler();
            DropHandler = new DropHandler();
        }

        #endregion

        #region Public Events

        /// <summary>
        /// The focused changed.
        /// </summary>
		  public event Action<IView> FocusedChanged;

		  /// <summary>
		  ///  Cancels wating process.
		  /// </summary>
		  public event Action<IView> WaitCancelled;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the copy source.
        /// </summary>
        public ICopySource CopySource
        {
            get
            {
                return DragHandler.Source;
            }

            set
            {
                DragHandler.Source = value;
            }
        }

        /// <summary>
        /// Gets or sets the paste target.
        /// </summary>
        public IPasteTarget PasteTarget
        {
            get
            {
                return DropHandler.Target;
            }

            set
            {
                DropHandler.Target = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether read only.
        /// </summary>
        public virtual bool ReadOnly
        {
            get
            {
                return true;
            }

            set
            {
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the drag handler.
        /// </summary>
        protected DragHandler DragHandler { get; private set; }

        /// <summary>
        /// Gets the drop handler.
        /// </summary>
        protected DropHandler DropHandler { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The clear.
        /// </summary>
        public virtual void Clear()
        {
        }

		  public void AddWarning(string warning)
		  {
		  }

		  public void RemoveWarning()
		  {
		  }

		  public void AddError(string warning)
		  {
		  }

		  public void RemoveError()
		  {
		  }

		  public void AddHint(string warning)
		  {
		  }

		  public void RemoveHint()
		  {
		  }

		  public void StartWait(IUseCase useCase)
		  {
		  }

		  public void EndWait(IUseCase useCase)
		  {
		  }

		  public void SetWaitProgress(IUseCase useCase)
		  {

		  }

        #endregion

        #region Methods

        /// <summary>
        /// The copy.
        /// </summary>
        protected virtual void Copy()
        {
            var data = CopySource.StartCopy();
            var copyDataObject = new CopyDataObject(data);
            Clipboard.SetDataObject(copyDataObject);
            CopySource.FinishCopy(CopyAction.Copy);
        }

        /// <summary>
        /// The cut.
        /// </summary>
        protected virtual void Cut()
        {
            var data = CopySource.StartCopy();
            var copyDataObject = new CopyDataObject(data);
            Clipboard.SetDataObject(copyDataObject);
            CopySource.FinishCopy(CopyAction.Move);
        }

        /// <summary>
        /// The on focused changed.
        /// </summary>
        protected virtual void OnFocusedChanged()
        {
            if (FocusedChanged != null)
            {
                FocusedChanged(this);
            }
        }

        /// <summary>
        /// The on got focus.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            OnFocusedChanged();
        }

        /// <summary>
        /// The on lost focus.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            OnFocusedChanged();
        }

        /// <summary>
        /// The paste.
        /// </summary>
        protected virtual void Paste()
        {
            var copyData = new PasteDataObject(Clipboard.GetDataObject());
            PasteTarget.Paste(copyData, CopyAction.Copy);
        }

        /// <summary>
        /// The copy menu item_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CopyMenuItem_Click(object sender, EventArgs e)
        {
            Copy();
        }

        /// <summary>
        /// The cut menu item_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CutMenuItem_Click(object sender, EventArgs e)
        {
            Cut();
        }

        /// <summary>
        /// The paste menu item_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void PasteMenuItem_Click(object sender, EventArgs e)
        {
            Paste();
        }

        #endregion
    }
}